

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client extends Thread 
 {
  private Tampon tamponDeTrace;
  private Process process;
  private int numPort;
    
  Client(Process _process, int _numPort)
   {
    this.process = _process;
    this.tamponDeTrace = process.getTamponDeTrace();
    this.numPort = _numPort;
   }
    
  @Override
  public void run()
   {
	  try
     {
      Socket socket = null;
      ObjectOutputStream fluxDeSortie = null;
      boolean test=true;
            
      while (test)
       {
        socket = new Socket (InetAddress.getByName("localhost"), numPort);
        fluxDeSortie = new ObjectOutputStream (socket.getOutputStream());
        System.out.println (process.getNumProcess() + " : Client    : Connexion au serveur SVG et attente d'un message de trace");
        Message message = this.tamponDeTrace.getElementTampon();
        System.out.println (process.getNumProcess() + " : Client    : Recuperation de : " + message);
        fluxDeSortie.writeObject (message);
        fluxDeSortie.flush();
        System.out.println (process.getNumProcess() + " : Client    : Envoi de : " + message);
        socket.close();
        MessageOrdre messageTest = (MessageOrdre) message;
        if(messageTest.getIntitule().equals("Fin"))
        {
        	System.out.println("Fermeture du thread client du processus " + process.getNumProcess());
        	test=false;
        }
       }
     }
    catch (ConnectException e)
     {
      System.err.println(e.getMessage());
     }
    catch (UnknownHostException e)
     {
      System.err.println(e.getMessage());
     }
    catch (IOException e)
     {
      System.err.println(e.getMessage());
     }
   }
 }
